package top.micheal.demo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @program: netty-parent
 * @description: HelloWorldServer
 * @author: Micheal.Wang
 * @blog: <a href="http://www.micheal.top">MichealWang</a>
 * @create: 2019-11-18 11:49
 **/
public class HelloWorldServer {
    public static void main(String[] args) {
        //创建Master Selector 处理的线程
        EventLoopGroup boosGroup = new NioEventLoopGroup(1);
        //创建Slave Selector读写 工作线程 默认是CPU核数*2
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {

            ServerBootstrap bootstrap = new ServerBootstrap().group(boosGroup, workGroup)
                    // 指定SSC 使用类。
                    .channel(NioServerSocketChannel.class)
                    // 指定pipLine初始化顺序
                    .childHandler(new TestServerLnitializer());

            //绑定端口并同步阻塞
            ChannelFuture channelFuture = bootstrap.bind(8096).sync();
            //关闭服务端。并阻塞执行
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            boosGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
